<?php
namespace App\Jobs;

use DB;
use App\Jobs\Job;

use Illuminate\Contracts\Bus\SelfHandling;
use Illuminate\Contracts\Queue\ShouldQueue;

require __DIR__.'/../Library/getui/IGt.Push.php';

use IGeTui;
use IGtTransmissionTemplate;
use IGtListMessage;
use IGtTarget;

use IGtAPNTemplate;
use IGtSingleMessage;

class SendPush extends Job implements SelfHandling, ShouldQueue
{
	protected $id;
	protected $transmissionData;
	protected $clients;
    /**
     * Create a new job instance.
     *
     * @param  User  $user
     * @return void
     */
    public function __construct($data)
    {
    	$this->id 					= $data["id"];
    	$this->clients 				= $data["clients"];

        $data["transmissionData"]["time"] = date("Y-m-d H:i:s");
        $this->transmissionData     = $data["transmissionData"];
    }

    /**
     * Execute the job.
     *
     * @param  Mailer  $mailer
     * @return void
     */
    public function handle()
    {
    	if(!isset($this->clients)) return false;
        //个推接口定义
        $IGtConfig = [
            'HOST'        =>'http://sdk.open.api.igexin.com/apiex.htm',
            'APPKEY'      => env("GETUI_APPKEY"),
            'MASTERSECRET'=> env("GETUI_MASTERSECRET"),
            'APPID'       => env("GETUI_APPID")
        ];

        putenv("needDetails=true");
        $igt = new IGeTui($IGtConfig['HOST'],$IGtConfig['APPKEY'],$IGtConfig['MASTERSECRET']);

        //IOS APNS推送
        if(strlen($this->clients[0]) == 64){
            $template = new IGtAPNTemplate();
            $template->set_pushInfo("", 1, $this->transmissionData['title'], "ptime.mp3", json_encode($this->transmissionData), "", "", "");
            //单个用户推送接口
            $message = new IGtSingleMessage();
            $message->set_data($template);
            //发送
            $sendTime = date("Y-m-d H:i:s");
            try{
                $result = $igt->pushAPNMessageToSingle($IGtConfig['APPID'], $this->clients[0], $message);
                $result = ["send_code"=>"1","send_time"=>$sendTime,"send_detail"=>json_encode($result)];
            }catch(Exception $e){
                $result = ["send_code"=>"-1","send_time"=>$sendTime,"send_detail"=>json_encode($e)];
            }
            DB::table("im_inform_user_method")->where("id",$this->id)->update($result);
            return true;
        }

        //透传消息模版
        $template =  new IGtTransmissionTemplate();
        $template->set_appId($IGtConfig['APPID']);//应用appid
        $template->set_appkey($IGtConfig['APPKEY']);//应用appkey

        $template->set_transmissionType(2);//透传消息类型
        $template->set_transmissionContent(json_encode($this->transmissionData));//透传内容

        //个推信息体
        $message = new IGtListMessage();
        $message->set_isOffline(true);//是否离线
        $message->set_offlineExpireTime(60*5*1000);//离线时间
        $message->set_data($template);//设置推送消息类型
        $message->set_PushNetWorkType(0);//设置是否根据WIFI推送消息，1为wifi推送，0为不限制推送
        $contentId = $igt->getContentId($message);//根据TaskId设置组名，支持下划线，中文，英文，数字

        //接收方
        foreach ($this->clients as $key => $client) {
            if(isset($client) && $client != ""){
                $target = new IGtTarget();
                $target->set_appId($IGtConfig['APPID']);
                $target->set_clientId($client);
                $targetList[] = $target;
            }
        }
        
        //发送
        $sendTime = date("Y-m-d H:i:s");
        if(isset($targetList)){
            try{
                $result = $igt->pushMessageToList($contentId, $targetList);
                $result = ["send_code"=>"1","send_time"=>$sendTime,"send_detail"=>json_encode($result)];
            }catch(Exception $e){
                $result = ["send_code"=>"-1","send_time"=>$sendTime,"send_detail"=>json_encode($e)];
            }
        }else{
            $result = ["send_code"=>"-2","send_time"=>$sendTime,"send_detail"=>""];
        }
        DB::table("im_inform_user_method")->where("id",$this->id)->update($result);
        return true; 
    }
}